<?php

namespace backend\controllers;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Goods;
use backend\models\User;
use yii\rbac\Role;

/**
 * GoodsSearch represents the model behind the search form of `backend\models\Goods`.
 */
class GoodsSearch extends Goods
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'gys', 'class', 'upfee', 'check','export'], 'integer'],
            [['goodsid','goodsno', 'goodsname', 'createtime', 'remark'], 'safe'],
            [['inprice', 'lastprice'], 'number'],

        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Goods::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['updatetime' => SORT_DESC]], // 新增配置项 默认 id 倒序
            'pagination'=>[
                'pageSize'=>20,

            ],
        ]);

        $this->load($params);
        if(!empty($params) && isset( $params['GoodsSearch'])){

            $goodssearch = $params['GoodsSearch'];
        }

//        var_dump($goodssearch);
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions

        $userid=Yii::$app->user->id;

        if(Yii::$app->authManager->checkAccess($userid,'商品列表') && $this->gys){
            $query->andFilterWhere([
                'gys'=>$this->gys
            ]);
        }
        if(Yii::$app->authManager->checkAccess($userid,'商品列表_供应商') ){
            $query->andFilterWhere([
                'gys'=>$userid
            ]);
        }
        if(Yii::$app->authManager->checkAccess($userid,'商品列表_运营')){
            $query->andFilterWhere([
                'nextid'=>$userid
            ]);
        }

        if(Yii::$app->authManager->checkAccess($userid,'商品列表_下辖一级审批')){


            $user = new User();
            $users = $user->find()->where(['parentid'=>$userid])->select('id')->asArray()->all();
            foreach($users as $item){
                $u[]=$item['id'];
            }

            $query->andWhere(['in','nextid',$u]);




        }
        if($this->goodsid){
            $query->andFilterWhere(['like', 'goodsid', $this->goodsid]);
        }
        if($this->goodsno){
            $query->andFilterWhere(['like', 'goodsno', $this->goodsno]);
        }
//        if($this->goodsname){
//            $query->andfilterWhere(['like','goodsname',$this->goodsname]);
//        }
        if($this->gys){
            $query->andFilterWhere(['gys'=>$this->gys]);
        }
        if($this->class){
            $query->andFilterWhere(['class'=>$this->class]);
        }
        if($this->upfee==1){
            $query->andfilterWhere(['>','upfee','0']);
        }elseif($this->upfee==2){
            $query->andFilterWhere(['<','upfee','0']);
        }

        if($this->check<5){
            $query->andFilterWhere(['check'=>$this->check]);
        }
        if(isset($goodssearch['datefrom']) && $goodssearch['datefrom']){
            $query->andFilterWhere(['>','updatetime',$goodssearch['datefrom']]);
        }
        if(isset($goodssearch['dateto']) && $goodssearch['dateto']){
            $query->andFilterWhere(['<','updatetime',$goodssearch['dateto']]);
        }
        if($this->export != 2){
            $query->andFilterWhere(['export'=>$this->export]);
        }




//           $query->orderBy('updatetime desc');
        //            // 输出SQL语句
//        $commandQuery = clone $query;
//        echo $commandQuery->createCommand()->getRawSql();
        return $dataProvider;
    }
}
